package com.oem.fragment.utils;

import android.content.Context;
import android.graphics.Color;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.EditText;
import android.util.Log;

import java.util.LinkedHashMap;
import java.util.Map;

public class FormUtils {

    // 构建上传参数表单字段
    public static Map<String, EditText> buildUploadFields(Context context) {
        Map<String, EditText> map = new LinkedHashMap<>();

        map.put("tags", createEditText(context, "标签（例如：Tag1,Tag2）", InputType.TYPE_CLASS_TEXT));
        map.put("category", createEditText(context, "类别（例如：Retail）", InputType.TYPE_CLASS_TEXT));
        map.put("budget", createEditText(context, "*预算（例如：5000.00）", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL));

        EditText startDate = createEditText(context, "*开始时间", InputType.TYPE_CLASS_TEXT );
        EditText endDate = createEditText(context, "*结束时间", InputType.TYPE_CLASS_TEXT);
        bindDateTimePicker(context, startDate);
        bindDateTimePicker(context, endDate);
        map.put("startDate", startDate);
        map.put("endDate", endDate);

        map.put("description", createEditText(context, "描述", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD));
        map.put("remark", createEditText(context, "备注", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD));

        return map;
    }

    // 创建单个输入框组件
    public static EditText createEditText(Context context, String hint, int inputType) {
        EditText et = new EditText(context);
        et.setInputType(inputType);

        if (hint.startsWith("*")) {
            SpannableString ss = new SpannableString(hint);
            ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            et.setHint(ss);
        } else {
            et.setHint(hint);
        }

        return et;
    }


    // 辅助方法，安全地获取 EditText 的文本，并提供默认值
    public static String getTextFromEditText(EditText editText, String defaultValue) {
        if (editText != null && editText.getText() != null && !editText.getText().toString().isEmpty()) {
            return editText.getText().toString().trim(); // 添加 trim() 去除首尾空格
        }
        return defaultValue;
    }

    private static void bindDateTimePicker(Context context, EditText editText) {
        editText.setFocusable(false); // 禁止手动输入
        editText.setOnClickListener(v -> {
            final java.util.Calendar calendar = java.util.Calendar.getInstance();

            new android.app.DatePickerDialog(context, (view, year, month, day) -> {
                int finalMonth = month + 1;
                new android.app.TimePickerDialog(context, (timeView, hour, minute) -> {
                    String dateTime = String.format("%04d-%02d-%02dT%02d:%02d:%02d", year, finalMonth, day, hour, minute, 0);
                    Log.d("DATEEEE", dateTime);

                    editText.setText(dateTime);
                }, calendar.get(java.util.Calendar.HOUR_OF_DAY), calendar.get(java.util.Calendar.MINUTE), true).show();

            }, calendar.get(java.util.Calendar.YEAR), calendar.get(java.util.Calendar.MONTH), calendar.get(java.util.Calendar.DAY_OF_MONTH)).show();
        });
    }

}

